package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.BCompany;
import com.ruoyi.system.domain.BCompanyUser;
import com.ruoyi.system.domain.vo.BCompanyVo;
import com.ruoyi.system.domain.vo.BUserVo;
import com.ruoyi.system.service.BCompanyService;
import com.ruoyi.system.mapper.BCompanyMapper;
import com.ruoyi.system.service.BCompanyUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author yangxingze
* @description 针对表【b_company(药企表)】的数据库操作Service实现
* @createDate 2024-12-28 00:45:04
*/
@Service
public class BCompanyServiceImpl extends ServiceImpl<BCompanyMapper, BCompany>
    implements BCompanyService{

    @Autowired
    BCompanyUserService companyUserService;

    /**
     * 保存药企信息
     * @param companyVo
     * @return
     */
    @Override
    public boolean saveVo(BCompanyVo companyVo) {
        List<BUserVo> userVoList = companyVo.getUserList();
        BCompany bCompany = new BCompany();
        BeanUtils.copyProperties(companyVo, bCompany);
        bCompany.setCreateTime(new Date());
        boolean result = this.save(bCompany);
        if(ObjectUtils.isNotEmpty(userVoList)){
            for(BUserVo userVo : userVoList){
                BCompanyUser companyUser = new BCompanyUser();
                companyUser.setCompanyId(bCompany.getId());
                companyUser.setDoctorId(userVo.getId());
                companyUser.setCreateTime(new Date());
                companyUserService.save(companyUser);
            }
        }
        return result;
    }

    /**
     * 更新药企信息
     * @param companyVo
     * @return
     */
    @Override
    public boolean updateVo(BCompanyVo companyVo) {
        List<BUserVo> userVoList = companyVo.getUserList();
        BCompany bCompany = new BCompany();
        BeanUtils.copyProperties(companyVo, bCompany);
        bCompany.setUpdateTime(new Date());
        boolean result = this.updateById(bCompany);
        // 删除之前的用户
        companyUserService.deleteByCompanyId(bCompany.getId());
        if(ObjectUtils.isNotEmpty(userVoList)){
            for(BUserVo userVo : userVoList){
                BCompanyUser companyUser = new BCompanyUser();
                companyUser.setCompanyId(bCompany.getId());
                companyUser.setDoctorId(userVo.getId());
                companyUser.setCreateTime(new Date());
                companyUserService.save(companyUser);
            }
        }
        return result;
    }

    /**
     * 根据条件查询药企列表
     * @param company
     * @return
     */
    @Override
    public List<BCompanyVo> selectCompanyList(BCompany company) {
        return baseMapper.selectCompanyList(company);
    }

    /**
     * 根据id查询药企
     * @param id
     * @return
     */
    @Override
    public BCompanyVo selectCompanyById(Long id) {
        return baseMapper.selectCompanyById(id);
    }

    /**
     * 删除药企
     * @param ids
     * @return
     */
    @Override
    public int deleteCompanyByIds(Long[] ids) {
        int result = baseMapper.deleteCompanyByIds(ids);
        if(result > 0){
            // 同时删除药企下的用户
            for(Long companyId: ids){
                companyUserService.deleteByCompanyId(companyId);
            }
        }
        return result;
    }
}




